﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ECCSystem
{
    public partial class frmEntryMain : Form
    {
        DataSet objDS = new DataSet();
        BizRule.ClsEntry objBREntry;
        public frmEntryMain()
        {
            InitializeComponent();
        }

        private void frmEntryMain_Load(object sender, EventArgs e)
        {
            try
            {
                this.BindDataGrid();
                cboFilter.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void frmEntryMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmMain = new frmMain();
            frmMain.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Form frmEAdd = new frmEntryAdd();
            frmEAdd.Show();
            this.Dispose();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                if (dgList[3, dgList.CurrentRow.Index].Value.ToString() == "保留")
                {
                    MessageBox.Show("保留のデータは変更することができません。", "ECC Information");
                    return;
                }
                BizRule.ClsCommon.gintBusinessID = Convert.ToInt32(dgList[0, dgList.CurrentRow.Index].Value.ToString());
                Form frmEEdit = new frmEntryEdit();
                frmEEdit.Show();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        //Bind data
        private void BindDataGrid()
        {
            objBREntry = new ECCSystem.BizRule.ClsEntry();
            objDS = (DataSet)objBREntry.GetList();
            dgList.DataSource = objDS.Tables[0];
            //Set different status numbers at bottom of form
            lblSumCount.Text = objDS.Tables[0].Rows.Count.ToString();
            DataSet objEnd = new DataSet();
            objEnd = objDS.Copy();
            objEnd.Tables[0].DefaultView.RowFilter = "BizStatus='終了'";
            lblEndStatusCount.Text = objEnd.Tables[0].DefaultView.Count.ToString();
            lblHoldStatusCount.Text = Convert.ToString(Convert.ToInt32(lblSumCount.Text.ToString()) - Convert.ToInt32(lblEndStatusCount.Text.ToString()));
            objEnd.Clear();
            objEnd.Dispose();
        }

        private void cboFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataSet objChanged = new DataSet();
                if (cboFilter.SelectedIndex != 0)
                {
                    objChanged = objDS.Copy();
                    objChanged.Tables[0].DefaultView.RowFilter = "BizStatus='" + cboFilter.Text.ToString() + "'";
                    dgList.DataSource = objChanged.Tables[0].DefaultView;
                }
                else
                {
                    dgList.DataSource = objDS.Tables[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int intBusinessID;
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                intBusinessID = Convert.ToInt32(dgList[0, dgList.CurrentRow.Index].Value.ToString());
                if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    objBREntry = new ECCSystem.BizRule.ClsEntry();
                    objBREntry.DeleteEntry(intBusinessID);
                    this.BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void dgList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (dgList[3, dgList.CurrentRow.Index].Value.ToString() == "保留")
                {
                    BizRule.ClsCommon.gintBusinessID = Convert.ToInt32(dgList[0, dgList.CurrentRow.Index].Value.ToString());
                    Form frmAdd = new frmEntryAdd();
                    frmAdd.Show();
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

    }
}